package com.woniu.microcommunityserver.book.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.microcommunityserver.activity.entity.ActivityManagementInfoEntity;
import com.woniu.microcommunityserver.book.entity.BorrowRevertInformationInfoEntity;
import com.woniu.microcommunityserver.book.service.BookInformationInfoService;
import com.woniu.microcommunityserver.book.service.BorrowRevertInformationInfoService;
import com.woniu.microcommunityserver.res.ResponseMsg;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("borrow")
public class BorrowRevertInformationInfoController {
    @Resource
    private BorrowRevertInformationInfoService borrowRevertInformationInfoService;

    @Resource
    private BookInformationInfoService bookInformationInfoService;
    @GetMapping("findAll")
    public ResponseMsg findAll(int pageNum){
        PageHelper.startPage(pageNum,10);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<BorrowRevertInformationInfoEntity> borrow = borrowRevertInformationInfoService.findAll();
        PageInfo<BorrowRevertInformationInfoEntity> pageInfo = new PageInfo<BorrowRevertInformationInfoEntity>(borrow);
        return ResponseMsg.builder().code(200).msg("所有借阅记录查询成功！").data(pageInfo).build();
    }

    @GetMapping("findAny")
    public ResponseMsg findAny(int pageNum,BorrowRevertInformationInfoEntity borrowRevertInformationInfoEntity){
        PageHelper.startPage(pageNum,10);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        Map<String,Object> map =new HashMap<>();
        if (borrowRevertInformationInfoEntity.getUserName()==""){
            borrowRevertInformationInfoEntity.setUserName(null);
        }
        map.put("userName",borrowRevertInformationInfoEntity.getUserName());
        map.put("userPhone",borrowRevertInformationInfoEntity.getUserPhone());
        map.put("borrowRevertState",borrowRevertInformationInfoEntity.getBorrowRevertState());
        System.out.println(borrowRevertInformationInfoEntity.getBorrowRevertState());
        List<BorrowRevertInformationInfoEntity> borrows = borrowRevertInformationInfoService.findAny(map);
        PageInfo<BorrowRevertInformationInfoEntity> pageInfo = new PageInfo<BorrowRevertInformationInfoEntity>(borrows);
        return ResponseMsg.builder().code(200).msg("指定借阅记录查询成功！").data(pageInfo).build();
    }

    @PostMapping("loan")
    public ResponseMsg insertBorrow(@RequestBody BorrowRevertInformationInfoEntity borrowRevertInformationInfoEntity){
        borrowRevertInformationInfoEntity.setBorrowRevertState(0);
        boolean flag = borrowRevertInformationInfoService.insertBorrow(borrowRevertInformationInfoEntity);
        if (flag){
            return ResponseMsg.builder().code(200).msg("借出成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("借出失败！").build();
        }
    }

    @PostMapping("back")
    public ResponseMsg updateState(@RequestBody BorrowRevertInformationInfoEntity borrowRevertInformationInfoEntity) {
        System.out.println(borrowRevertInformationInfoEntity);
        boolean flag = borrowRevertInformationInfoService.updateState(borrowRevertInformationInfoEntity.getId(),borrowRevertInformationInfoEntity.getRevertTime());
        boolean flag1 = bookInformationInfoService.backBook(borrowRevertInformationInfoEntity.getFkBookId());
        if (flag && flag1){
            return ResponseMsg.builder().code(200).msg("归还成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("归还失败！").build();
        }
    }
}
